/**
 * 
 */
package vehicleController;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import constants.Constants;
import userController.Navigator;
import vehicleEntity.RentalVehicleEntity;
import vehicleEntity.SaleVehicleEntity;
import vehicleLogic.ViewVehicleLogic;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;


/**
 * @author Yokesh Kumar
 * @author Sashiraj Dhanarajan
 *
 */
public class ViewVehicleController implements Initializable{

	@FXML
	private TableColumn<SaleVehicleEntity, Integer> vehicleId;
	@FXML
	private TableColumn<SaleVehicleEntity, String> regNo;
	@FXML
	private TableColumn<SaleVehicleEntity, String> vehType;
	@FXML
	private TableColumn<SaleVehicleEntity, String> vehClass;
	@FXML
	private TableColumn<SaleVehicleEntity, String> vehName;
	@FXML
	private TableColumn<SaleVehicleEntity, Double> vehAmount;
	@FXML
	private TableView<SaleVehicleEntity> saleTable;
	@FXML
	private ComboBox<String> vehicleClass;
	@FXML
	private ComboBox<String> vehicleType;
	@FXML
	private ComboBox<String> status;
	@FXML
	private TableColumn<RentalVehicleEntity, Integer>vehId;
	@FXML
	private TableColumn<RentalVehicleEntity, String>vehicleName;
	@FXML
	private TableColumn<RentalVehicleEntity, Integer>rentId;
	@FXML
	private TableColumn<RentalVehicleEntity, String>vehStatus;
	@FXML
	private TableView<RentalVehicleEntity>vehicleStatus;
	@FXML
	private Label msg;
	@FXML
	private Label vehicleTypeLabell;
	@FXML
	private Label statusLabel;




	private static ObservableList<SaleVehicleEntity> vehicleSaleList;
	private static ObservableList<RentalVehicleEntity> statusList;
	/**
	 * This method will be used by both customer and clerk
	 */
	public void viewSaleList(){
		ViewVehicleLogic vehicleLogic = new ViewVehicleLogic();
		vehicleSaleList = vehicleLogic.fetchSaleList();
		Navigator.setInitailize(true);
		Navigator.loadStackPane(Navigator.SALE_LIST_VIEW_FXML);
		vehicleSaleList = null;
	}

	public void viewVehicleStatus(){
		// load frame to choose vehicle class
	}

	@FXML
	public void fetchvehicles(ActionEvent event){
		// fetchVehicleStatus()
	}


	@Override
	public void initialize(URL location, ResourceBundle resources) {
		if(Navigator.isInitailize() && vehicleSaleList != null){
			vehicleId.setCellValueFactory(new PropertyValueFactory<SaleVehicleEntity, Integer>("saleVehicleId"));
			regNo.setCellValueFactory(new PropertyValueFactory<SaleVehicleEntity, String>("regNo"));
			vehType.setCellValueFactory(new PropertyValueFactory<SaleVehicleEntity, String>("vehicleType"));
			vehClass.setCellValueFactory(new PropertyValueFactory<SaleVehicleEntity, String>("vehicleClass"));
			vehName.setCellValueFactory(new PropertyValueFactory<SaleVehicleEntity, String>("vehicleName"));
			vehAmount.setCellValueFactory(new PropertyValueFactory<SaleVehicleEntity, Double>("saleAmount"));
			saleTable.getItems().setAll(vehicleSaleList);
			Navigator.setInitailize(false);
		}
	}
	/**
	 * The method fetches and show the vehicle types based on the vehicle class
	 * @param event
	 * @pre The vehicle class should be selected
	 * @post Display the vehicle types
	 */
	@FXML
	public void fetchVehicleType(ActionEvent event){
		if(vehicleClass.getValue() != null && !vehicleClass.getValue().isEmpty()){
			ViewVehicleLogic logic = new ViewVehicleLogic();
			if(vehicleClass.getValue().equalsIgnoreCase(Constants.CAR) || 
					vehicleClass.getValue().equalsIgnoreCase(Constants.TRUCK)){
				vehicleStatus.setVisible(false);
				List<String> vehiceTypeList = logic.fetchVehicleType(vehicleClass.getValue());
				vehicleType.getItems().clear();
				vehicleType.getItems().addAll(vehiceTypeList);
				vehicleType.setVisible(true);
				vehicleTypeLabell.setVisible(true);
				statusLabel.setVisible(true);
				status.setVisible(true);
			}
			else{
				vehicleType.setVisible(false);
				vehicleTypeLabell.setVisible(false);
				status.setVisible(false);
				statusLabel.setVisible(false);
				vehicleStatus.setVisible(false);				
			}
		}

	}
	/**
	 * This method  displays vehicle details based on the vehicle class, vehicle type and status.
	 * @pre The vehicle class,vehicle type nad status should be selected
	 * @post Display the vehicle details for the selected status
	 */

	@FXML
	public void fetchVehicles(ActionEvent event){
		System.out.println("fetching vehicle status");
		ViewVehicleLogic logic = new ViewVehicleLogic();
		if(vehicleClass.getValue() != null && !vehicleClass.getValue().isEmpty()){
			if(vehicleType.getValue() !=null && !vehicleType.getValue().isEmpty()){
				if(status.getValue() !=null && !status.getValue().isEmpty()){
					statusList = logic.fetchVehiclesTypeBasedOnStatus(vehicleType.getValue(),status.getValue());
					System.out.println("statusListsize " + statusList.size());
					populateTable();
					vehicleStatus.setVisible(true);
				}
				else{
					statusList = logic.fetchAllVehicles(vehicleType.getValue());
					System.out.println("statusListsize " + statusList.size());
					populateTable();
					vehicleStatus.setVisible(true);
				}
			}
			else{
				if(status.getValue() !=null && !status.getValue().isEmpty()){
					statusList = logic.fetchVehiclesBasedOnStatus(vehicleClass.getValue(), status.getValue());
					System.out.println("statusListsize " + statusList.size());
					populateTable();
					vehicleStatus.setVisible(true);
				}			
			}
		}
		else{

		}
	}
	/**
	 * This method populates all the vehicle into the table
	 * @pre The Vehicle class and Vehicle Type should be selected
	 * @post Display the table with the vehicle status
	 */
	public void populateTable(){
		if(statusList != null && statusList.size() >0){
			vehId.setCellValueFactory(new PropertyValueFactory<RentalVehicleEntity, Integer>("vehicleId"));
			vehicleName.setCellValueFactory(new PropertyValueFactory<RentalVehicleEntity, String>("vehicleName"));
			rentId.setCellValueFactory(new PropertyValueFactory<RentalVehicleEntity, Integer>("rentId"));
			vehStatus.setCellValueFactory(new PropertyValueFactory<RentalVehicleEntity, String>("status"));
			vehicleStatus.getItems().setAll(statusList);
		}
		else{
			vehicleStatus.getItems().clear();
		}
	}


}
